'use server';

import Head from 'next/head';
import Image from 'next/image';
import styles from '../styles/Home.module.css';

import { useDrag, useDrop } from 'react-dnd';
import { useRef, useState, useEffect } from 'react';

import { Button } from '@arco-design/web-react';
import { Button as AntdButton } from 'antd';

import '@arco-design/web-react/dist/css/arco.css';

const container = [
  <a href="https://nextjs.org/docs" className={styles.card} key="1">
    <h2>Documentation &rarr;</h2>
    <p>Find in-depth information about Next.js features and API.</p>
  </a>,
  <a href="https://nextjs.org/learn" className={styles.card} key="2">
    <h2>Learn &rarr;</h2>
    <p>Learn about Next.js in an interactive course with quizzes!</p>
  </a>,
  <a href="https://github.com/vercel/next.js/tree/canary/examples" className={styles.card} key="12">
    <h2>Examples &rarr;</h2>
    <p>Discover and deploy boilerplate example Next.js projects.</p>
  </a>,
  <a
    href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
    className={styles.card}
    key="122"
  >
    <h2>Deploy &rarr;</h2>
    <p>Instantly deploy your Next.js site to a public URL with Vercel.</p>
  </a>,
];

const DragItem = function ({ index }) {
  const ref = useRef(null);

  const [, drag] = useDrag({
    type: 'box',
    item: {
      id: index,
    },
  });

  useEffect(() => {
    drag(ref);
  }, [ref, drag]);

  return <div ref={ref}>{container[index]}</div>;
};

function Document() {
  const dropRef = useRef(null);

  const [dropBoxes, setDropBoxes] = useState([]);

  useDrop(() => {
    return {
      accept: 'box',
      drop(item) {
        setDropBoxes((boxes) => [...boxes, item]);
      },
    };
  });

  return (
    <div className={styles.container}>
      <Button>Arco</Button>
      <AntdButton>Antd</AntdButton>

      <Head>
        <title>Create Next App</title>
        <meta name="description" content="Generated by create next app" />
        <link rel="icon" href="/favicon.ico" />
      </Head>

      <main className={styles.main}>
        {/* H1是target元素 */}
        <h1 className={styles.title} ref={dropRef} style={{ border: '1px solid black' }}>
          Welcome to <a href="https://nextjs.org">Next.js!</a>
          {dropBoxes.map((item) => {
            return <DragItem index={item.id} key={item.id} />;
          })}
        </h1>

        {/* 下面是若拽元素‘ */}
        <div className={styles.grid}>
          {container.map((_, index) => {
            return <DragItem index={index} key={index} />;
          })}
        </div>
      </main>

      <footer className={styles.footer}>
        <a
          href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
          target="_blank"
          rel="noopener noreferrer"
        >
          Powered by{' '}
          <span className={styles.logo}>
            <Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
          </span>
        </a>
      </footer>
    </div>
  );
}

export default Document;
